using imitateMacQQ.WebApi.Configures;
using imitateMacQQ.WebApi.Domain.CircleServe.Entity.dao;
using imitateMacQQ.WebApi.Domain.CircleServe.Entity.dto;
using imitateMacQQ.WebApi.Domain.CircleServe.Interface;
using imitateMacQQ.WebApi.Domain.Request.utils;
using imitateMacQQ.WebApi.Repository.EFCore.Model;
using imitateMacQQ.WebApi.Repository.EFCore.Model.Circle;
using Microsoft.EntityFrameworkCore;

namespace imitateMacQQ.WebApi.Domain.CircleServe.Service;

public class TalkService : ITalkInfo
{
    private readonly INLogHelper _logHelper;

    public TalkService(INLogHelper logHelper)
    {
        _logHelper = logHelper;
    }

    public async Task<Request<bool>> SendRemarks(TalkRemark dto)
    {
        try
        {
            await using DataDbContext db = new DataDbContext();
            //校验原数据是否被删除
            var isNotNullData = await db.FriendCircles.FirstOrDefaultAsync(e =>
                e.Remarks == dto.Remarks);
            if (isNotNullData is null || isNotNullData.IsDelete)
                throw new Exception("DATA DELETE");

            //Create ONLY UiD
            string uid = DateTime.Now.ToString("O") + dto.AcceptAccount;
            //SAVE SQL
            var targetData = await db.Remarks.FirstOrDefaultAsync(e =>
                e.Uid == uid);
            if (targetData is not null)
                throw new Exception("Send is Bad");
            var time = DateTime.Now;
            db.Remarks.Add(new Remark()
            {
                Remarks = dto.Remarks,
                SendAccount = dto.SendAccount,
                AcceptAccount = dto.AcceptAccount,
                Content = dto.Content,
                DateTime = time,
                Uid = uid
            });
            await db.SaveChangesAsync();
            var res = await SendMes(db, dto, uid, time);
            return res ? Request<bool>.Ok(true) : Request<bool>.Ok(false);
        }
        catch (Exception ex)
        {
            _logHelper.LogError(ex);
            return Request<bool>.Ok(false, false, ex.Message);
        }
    }

    public Task<Request<bool>> DelRemarks(string uid)
    {
        try
        {
            using DataDbContext db = new DataDbContext();
            var info = db.FriendCircles;
        }
        catch (Exception ex)
        {
            _logHelper.LogError(ex);
        }

        throw new NotImplementedException();
    }

    public async Task<Request<UsrTalk[]>> GetUsrTalk(string send, string accept, string remark)
    {
        try
        {
            await using DataDbContext db = new DataDbContext();
            var info = db.Remarks.Where(e => e.Remarks == remark);
            var res = new UsrTalk[info.Count()];
            int index = 0;
            foreach (var item in info)
            {
                res[index] = new UsrTalk()
                {
                    Send = item.SendAccount,
                    Accept = item.AcceptAccount,
                    Uid = item.Uid,
                    DateTime = item.DateTime
                };
                index++;
            }

            var isReadMes = db.RemarksMes.Where(e =>
                e.DateTime == res[res.Length].DateTime &&
                e.Remarks == remark);
            foreach (var item in isReadMes)
            {
                item.IsRead = true;
            }

            await db.SaveChangesAsync();
            return Request<UsrTalk[]>.Ok(res);
        }
        catch (Exception ex)
        {
            _logHelper.LogError(ex);
            return Request<UsrTalk[]>.Ok(null!);
        }
    }

    public async Task<Request<UsrTalk[]>> GetAllTalk(string remark)
    {
        try
        {
            await using DataDbContext db = new DataDbContext();
            var info = db.Remarks
                .Where(e => e.Remarks == remark).ToList();
            if (info.Count == 0)
                return Request<UsrTalk[]>.Ok(null!);
            var data = new UsrTalk[info.Count()];
            var index = 0;
            foreach (var item in info)
            {
                data[index] = new UsrTalk()
                {
                    Send = item.SendAccount,
                    Accept = item.AcceptAccount,
                    Content = item.Content,
                    DateTime = item.DateTime,
                    Uid = item.Uid
                };
                ++index;
            }
            return Request<UsrTalk[]>.Ok(data);
        }
        catch (Exception ex)
        {
            _logHelper.LogError(ex);
            return Request<UsrTalk[]>.Ok(null!, false, ex.Message);
        }
    }

    public async Task<bool> SendMes(DataDbContext db, TalkRemark dto, string uid, DateTime time)
    {
        try
        {
            var infoData = await db.RemarksMes.FirstOrDefaultAsync(e =>
                e.Uid == uid);
            if (infoData is not null)
                throw new Exception("REMARKS MES INFO_DATA IS NOT NULL");
            db.RemarksMes.Add(new RemarksMes()
            {
                Remarks = dto.Remarks,
                AcceptAccount = dto.AcceptAccount,
                SendAccount = dto.SendAccount,
                IsRead = false,
                DateTime = time
            });
            return true;
        }
        catch (Exception ex)
        {
            _logHelper.LogError(ex);
            return false;
        }
    }
}